package com.apes.fn.scm.used.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * 功能：旧件库存(正常库存;保险库存)
 *
 * @author BieBi
 * @create 2018-9-4 09:52:00
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = UsedStock.CONST_TABLE_NAME, indexes = {
        @Index(name = "u_acc_used_stock", columnList = "USED_ID,LOCATION_ID", unique = true)
})
public class UsedStock extends Model {
    public static final String CONST_TABLE_NAME = "acc_used_stock";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "旧件库存帐")
    private String id;  //内码

    @Comment("旧件")
    @ManyToOne(optional = false)
    @JoinColumn(name = "used_id", referencedColumnName = "id", nullable=false)
    private Used used;

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID", referencedColumnName = "id", nullable = false)
    private PartyRole company;   //公司编码

    @Comment("仓库")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "LOCATION_ID", referencedColumnName = "id", nullable = false)
    private Location location;  //仓库库区

    @Comment("标准库存数量")
    @Column(name = "stock_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double stockQty = 0; // 库存数量

    @Comment("保险库存数量")
    @Column(name = "insurance_stock_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double insuranceStockQty = 0; // 库存数量

    @Comment("最后入库日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LAST_IN_DATE")
    private Date lastInDate;  //最后入库日期

    @Comment("最后出库日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LAST_OUT_DATE")
    private Date lastOutDate;   //最后出库日期

    /*
     * 功能：更新旧件标准库存
     */
    public void addStockQty(double qty) {
        this.stockQty += qty;
        if(qty > 0) {
            lastInDate = new Date();
        }else if(qty < 0) {
            lastOutDate = new Date();
        }
        if (this.stockQty < 0) throw new RuntimeException("旧件标准库存不允许小于零！旧件编码：" + this.used.getId());
    }

    /*
     * 功能：更新旧件保险库存
     */
    public void addInsuranceStockQty(double qty) {
        this.insuranceStockQty += qty;
        if(qty > 0) {
            lastInDate = new Date();
        }else if(qty < 0) {
            lastOutDate = new Date();
        }
        if (this.stockQty < 0) throw new RuntimeException("旧件保险库存不允许小于零！旧件编码：" + this.used.getId());
    }


}